package com.lord.rowcopy.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * @Description: 获取模板文件的工具类
 * @author Lord
 * @date 2019年1月7日
 */
public class TemplateFileUtil {

    public static FileInputStream getTemplates(String tempName) throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("templates/" + tempName);
        InputStream inputStream = classPathResource.getInputStream();

        // 生成目标文件
        File targetFile = File.createTempFile("template_export_copy", ".xls");
        try {
            FileUtils.copyInputStreamToFile(inputStream, targetFile);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }

        return new FileInputStream(targetFile);
    }

    public static void main(String[] args) throws IOException {
        Resource resource = new ClassPathResource("templates/balance.xls");
        System.out.println(resource.getURI());
    }
}
